package com.fare.taluo.web.action.detect.sys;

import com.fare.taluo.web.action.detect.ComputeInfoAction;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Namespace;
import org.hyperic.sigar.*;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

/**
 * CPU资源监控
 * @author creator: xiangsf
 * @author modifior: xiangsf
 * @since 2012-12-11 下午1:51:22
 * @version v1.0
 */
@Namespace("/")
@Controller("cpu")
@Scope("prototype")
public class CpuAction extends ComputeInfoAction
{

	private static final long serialVersionUID = 0xf905e9931d9efa5L;
	private Sigar sigar;
	private int cpuCount;
	private double userUsed;
	private double sysUsed;
	private double idleUsed;
	private double waitUsed;
	private double niceUsed;
	private double combinedUsed;
	private double irqUsed;
	private double softIrq;
	private double stolen;
	private List<CpuDetect> cpus;

	public CpuAction()
	{
		sigar = null;
		cpuCount = 1;
		cpus = new ArrayList<CpuDetect>();
	}

	public String show()
		throws Exception
	{
		org.hyperic.sigar.CpuInfo infos[] = null;
		CpuPerc percs[] = null;
		try
		{
			sigar = new Sigar();
			setOutputCpuPer(sigar.getCpuPerc());
			setCpuCount(sigar.getCpuInfoList().length);
			infos = sigar.getCpuInfoList();
			percs = sigar.getCpuPercList();
			for (int i = 0; i < infos.length; i++)
				cpus.add(new CpuDetect((new StringBuilder("CPU")).append(i + 1).toString(), infos[i], percs[i]));
		}
		catch (SigarException e)
		{
			e.printStackTrace();
			logger.error("获取CPU信息出错!", e);
		}finally{
			if (sigar != null)
				sigar.close();
			infos = null;
			percs = null;
		}
		returnPageURL = "/system/detect/cpu.jsp";
		return "dispatcher";
	}

	private void setOutputCpuPer(CpuPerc totalPerc)
	{
		idleUsed = totalPerc.getIdle();
		combinedUsed = totalPerc.getCombined();
		softIrq = totalPerc.getSoftIrq();
		irqUsed = totalPerc.getIrq();
		niceUsed = totalPerc.getNice();
		stolen = totalPerc.getStolen();
		sysUsed = totalPerc.getSys();
		userUsed = totalPerc.getUser();
		waitUsed = totalPerc.getWait();
	}

	public int getCpuCount()
	{
		return cpuCount;
	}

	public void setCpuCount(int cpuCount)
	{
		this.cpuCount = cpuCount;
	}

	public List<CpuDetect> getCpus()
	{
		return cpus;
	}

	public void setCpus(List<CpuDetect> cpus)
	{
		this.cpus = cpus;
	}

	public double getUserUsed()
	{
		return userUsed;
	}

	public void setUserUsed(double userUsed)
	{
		this.userUsed = userUsed;
	}

	public double getSysUsed()
	{
		return sysUsed;
	}

	public void setSysUsed(double sysUsed)
	{
		this.sysUsed = sysUsed;
	}

	public double getIdleUsed()
	{
		return idleUsed;
	}

	public void setIdleUsed(double idleUsed)
	{
		this.idleUsed = idleUsed;
	}

	public double getWaitUsed()
	{
		return waitUsed;
	}

	public void setWaitUsed(double waitUsed)
	{
		this.waitUsed = waitUsed;
	}

	public double getNiceUsed()
	{
		return niceUsed;
	}

	public void setNiceUsed(double niceUsed)
	{
		this.niceUsed = niceUsed;
	}

	public double getCombinedUsed()
	{
		return combinedUsed;
	}

	public void setCombinedUsed(double combinedUsed)
	{
		this.combinedUsed = combinedUsed;
	}

	public double getIrqUsed()
	{
		return irqUsed;
	}

	public void setIrqUsed(double irqUsed)
	{
		this.irqUsed = irqUsed;
	}

	public double getSoftIrq()
	{
		return softIrq;
	}

	public void setSoftIrq(double softIrq)
	{
		this.softIrq = softIrq;
	}

	public double getStolen()
	{
		return stolen;
	}

	public void setStolen(double stolen)
	{
		this.stolen = stolen;
	}
}

class CpuDetect
{

	private String cpuId;
	private int mhz;
	private String vendor;
	private String model;
	private long cacheSize;
	private int totalCores;
	private int totalSockets;
	private int coresPerSocket;
	private double userUsed;
	private double sysUsed;
	private double idleUsed;
	private double waitUsed;
	private double niceUsed;
	private double combinedUsed;
	private double irqUsed;
	private double softIrq;
	private double stolen;

	public CpuDetect(String cpuId, CpuInfo info, CpuPerc perc)
	{
		this.cpuId = cpuId;
		cacheSize = info.getCacheSize();
		coresPerSocket = info.getCoresPerSocket();
		mhz = info.getMhz();
		model = info.getModel();
		totalCores = info.getTotalCores();
		totalSockets = info.getTotalSockets();
		coresPerSocket = info.getCoresPerSocket();
		vendor = info.getVendor();
		idleUsed = perc.getIdle();
		combinedUsed = perc.getCombined();
		softIrq = perc.getSoftIrq();
		irqUsed = perc.getIrq();
		niceUsed = perc.getNice();
		stolen = perc.getStolen();
		sysUsed = perc.getSys();
		userUsed = perc.getUser();
		waitUsed = perc.getWait();
	}

	public String getCpuId()
	{
		return cpuId;
	}

	public void setCpuId(String cpuId)
	{
		this.cpuId = cpuId;
	}

	public String getVendor()
	{
		return vendor;
	}

	public void setVendor(String vendor)
	{
		this.vendor = vendor;
	}

	public String getModel()
	{
		return model;
	}

	public void setModel(String model)
	{
		this.model = model;
	}

	public int getMhz()
	{
		return mhz;
	}

	public void setMhz(int mhz)
	{
		this.mhz = mhz;
	}

	public long getCacheSize()
	{
		return cacheSize;
	}

	public void setCacheSize(long cacheSize)
	{
		this.cacheSize = cacheSize;
	}

	public int getTotalCores()
	{
		return totalCores;
	}

	public void setTotalCores(int totalCores)
	{
		this.totalCores = totalCores;
	}

	public int getTotalSockets()
	{
		return totalSockets;
	}

	public void setTotalSockets(int totalSockets)
	{
		this.totalSockets = totalSockets;
	}

	public int getCoresPerSocket()
	{
		return coresPerSocket;
	}

	public void setCoresPerSocket(int coresPerSocket)
	{
		this.coresPerSocket = coresPerSocket;
	}

	public double getUserUsed()
	{
		return userUsed;
	}

	public void setUserUsed(double userUsed)
	{
		this.userUsed = userUsed;
	}

	public double getSysUsed()
	{
		return sysUsed;
	}

	public void setSysUsed(double sysUsed)
	{
		this.sysUsed = sysUsed;
	}

	public double getIdleUsed()
	{
		return idleUsed;
	}

	public void setIdleUsed(double idleUsed)
	{
		this.idleUsed = idleUsed;
	}

	public double getWaitUsed()
	{
		return waitUsed;
	}

	public void setWaitUsed(double waitUsed)
	{
		this.waitUsed = waitUsed;
	}

	public double getNiceUsed()
	{
		return niceUsed;
	}

	public void setNiceUsed(double niceUsed)
	{
		this.niceUsed = niceUsed;
	}

	public double getCombinedUsed()
	{
		return combinedUsed;
	}

	public void setCombinedUsed(double combinedUsed)
	{
		this.combinedUsed = combinedUsed;
	}

	public double getIrqUsed()
	{
		return irqUsed;
	}

	public void setIrqUsed(double irqUsed)
	{
		this.irqUsed = irqUsed;
	}

	public double getSoftIrq()
	{
		return softIrq;
	}

	public void setSoftIrq(double softIrq)
	{
		this.softIrq = softIrq;
	}

	public double getStolen()
	{
		return stolen;
	}

	public void setStolen(double stolen)
	{
		this.stolen = stolen;
	}
}